// 纪念我的初中数学老师
// 她姓龙
package com.gly091020;

import com.gly091020.config.MathDragonConfig;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.AreaEffectCloudEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.decoration.EndCrystalEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.random.Random;

import java.util.UUID;

public class MathDragon implements ModInitializer {
    public static final String ModID = "math_dragon";
    public static final UUID _5112151111121 = UUID.fromString("91bd580f-5f17-4e30-872f-2e480dd9a220");
    public static MathDragonConfig CONFIG;
    @Override
    public void onInitialize() {
        ServerPlayNetworking.registerGlobalReceiver(NetWorkingPackId.ToDragonRight, (minecraftServer, serverPlayerEntity, serverPlayNetworkHandler, packetByteBuf, packetSender) -> {
            Entity entity = serverPlayerEntity.getWorld().getEntityById(packetByteBuf.readInt());
            float amount = packetByteBuf.readFloat();
            serverPlayerEntity.sendMessage(Text.translatable(String.format("say.math_dragon.right%d", Random.create().nextBetween(1, 5))));
            serverPlayerEntity.playSound(SoundEvents.ENTITY_VILLAGER_YES, 1, 1);
            if(entity instanceof EndCrystalEntity){
                entity.kill();
            } else if (entity instanceof AreaEffectCloudEntity) {
                entity.kill();
            } else if (entity instanceof EnderDragonEntity) {
                float time_present = packetByteBuf.readFloat();
                entity.damage(entity.getWorld().getDamageSources().playerAttack(serverPlayerEntity), amount * time_present * 2);
            }
        });

        ServerPlayNetworking.registerGlobalReceiver(NetWorkingPackId.ToDragonWrong, (minecraftServer, serverPlayerEntity, serverPlayNetworkHandler, packetByteBuf, packetSender) -> {
            Entity entity = serverPlayerEntity.getWorld().getEntityById(packetByteBuf.readInt());
            serverPlayerEntity.sendMessage(Text.translatable(String.format("say.math_dragon.wrong%d", Random.create().nextBetween(1, 5))));
            serverPlayerEntity.playSound(SoundEvents.ENTITY_VILLAGER_NO, 1, 1);
            if (entity instanceof AreaEffectCloudEntity e){
                e.setRadius(e.getRadius() * 2);
            } else if(entity instanceof EnderDragonEntity enderDragonEntity){
                enderDragonEntity.setHealth(enderDragonEntity.getHealth() + 10);
            }
        });

        AutoConfig.register(MathDragonConfig.class, JanksonConfigSerializer::new);
        CONFIG = AutoConfig.getConfigHolder(MathDragonConfig.class).getConfig();

        AutoConfig.getConfigHolder(MathDragonConfig.class).registerSaveListener((configHolder, mathDragonConfig) -> {
            CONFIG = AutoConfig.getConfigHolder(MathDragonConfig.class).getConfig();
            return ActionResult.SUCCESS;
        });
    }
}
